const logger = require('../logger');
const db = require('../../db');
const TafCMD = require('@taf/taf-cmd')();

class MonitorConf {
    /*
    constructor() {

    }
    */

    /**
     * 告警配置列表
     */
    getMonitorConfList(req) {
        let monitor_id = '';
        let condition = {};
        if (req.params) {
            monitor_id = req.params.monitor_id;
            if (monitor_id) {
                condition = { monitor_id };
            }
        }
        logger.data.info('model.monitorConf.getMonitorConfList req：', req.params);
        return new Promise(function (resolve, reject) {
            // let condition = {};
            db.monitor_conf.findAndCountAll({
                // attributes: [],
                where: condition,
                order: [
                    ['update_time', 'desc']
                ],
                include: [
                    /*
                    {
                        model: db.grab_source_conf,
                        as: 'grabSourceConf'
                    },
                    */
                    {
                        model: db.crawler_storage_model,
                        as: 'crawlerStorageModel'
                    }
                ]
            }).then(function (result) {
                resolve(result);
            }, function (err) {
                logger.error.error('model.monitorConf.getMonitorConfList error：', err);
                reject(err);
            });
        });
    }

    /**
     * 告警配置
     */
    saveMonitorConf(req) {
        let reqData = req.body;
        let { monitor_id } = reqData;
        const now = new Date();
        const uid = req.userInfo.uid;
        const this_ = this;
        logger.data.info('model.monitorConf.saveMonitorConf req：', reqData);
        return new Promise(function (resolve, reject) {
            reqData.updator = uid;
            reqData.update_time = now;
            if (Number(monitor_id)) {
                logger.data.info('model.monitorConf.saveMonitorConf update：', reqData);
                db.monitor_conf.update(reqData, {
                    where: { monitor_id }
                }).then(result => {
                    this_.loadCrawlerMonitorExServer();
                    resolve(result[0]);
                }).catch(e => {
                    logger.error.error('model.monitorConf.saveMonitorConf update error：', e);
                    resolve('');
                });
            } else {
                reqData.creator = uid;
                reqData.creat_time = now;
                logger.data.info('model.monitorConf.saveMonitorConf create：', reqData);
                db.monitor_conf.create(reqData).then(result => {
                    this_.loadCrawlerMonitorExServer();
                    resolve(result);
                }).catch(e => {
                    logger.error.error('model.monitorConf.saveMonitorConf create error：', e);
                    resolve('');
                });
            }
        });
    }

    /**
     * 通知CrawlerMonitorExServer服务重新加载数据
     */
    loadCrawlerMonitorExServer() {
        TafCMD.notifySvr('News.CrawlerMonitorExServer.CrawlerMonitorExServerObj', 'restartUpdateSchedule').then((ret) => {
            logger.data.info('News.CrawlerMonitorExServer.CrawlerMonitorExServerObj-----restartUpdateSchedule success：', ret);
        }).catch(err => {
            logger.error.error('News.CrawlerMonitorExServer.CrawlerMonitorExServerObj-----restartUpdateSchedule error：', err);
        });
    }
}


module.exports = new MonitorConf();
